package blockingqueueDemo;

import java.util.LinkedList;
import java.util.Queue;

public class MyBlockingQueueObjectWait implements MyBlockingQueue{
    private Queue<Object> queue;
    private int limit;

    public MyBlockingQueueObjectWait(int limit) {
        this.limit = limit;
        this.queue = new LinkedList<Object>();
    }

    public synchronized void put(Object o) throws InterruptedException{
        while(queue.size() == limit){
            wait();
        }
        queue.add(o);
        notifyAll();
    }

    public  synchronized  Object get() throws InterruptedException{
        while (queue.size() == 0) {
            wait();
        }
        Object item = queue.remove();
        notifyAll();
        return item;
    }
    public  synchronized  int size(){
        return queue.size();
    }
}
